package Server.players;

import Server.map.Box;


public class Herbivorous extends Dino{

	public static final int MAX_DISTANCE = 2;
	public Herbivorous(int id){
		super(id);
	}
	public Herbivorous(int id, Box location){
		super(id, location);
	}
	public Herbivorous(Herbivorous e){
		super(e);
	}
	@Override
	public boolean equals(Object b) {
		if (b instanceof Herbivorous){
			Herbivorous h = (Herbivorous) b;
			if (this.idDino.equals((h.idDino)))
					return true;
		}
		return false;
	}
	@Override
	public int hashCode(){
		return 31 * this.idDino.hashCode();
	}
	@Override
	public boolean isCarniv() {
		return false;
	}
	@Override
	public boolean isHerb() {
		return true;
	}
	@Override
	public int maxDistance(){
		return Herbivorous.MAX_DISTANCE;
	}

	/*
	 * @requires foodBox != null
	 * @ensures foodBox.isVegetableBox() <==> this.getEnergy() >= \old(this.getEnergy()
	 */
	@Override
	public void eat(Box foodBox) {
		if (foodBox.isVegetableBox()){
			if (this.energy + foodBox.getFoodEnergy() > 1000 * this.dimension)
				this.energy = 1000 * this.dimension;
			else this.energy += foodBox.getFoodEnergy();
		}
	}
	
	/* 
	 * @requires d != null
	 * @ensures (\result == this || \result == d) &&
	 * ((* this più forte di d *) && (d instanceof Carnivorous)) => this.getEnergy == \old(this.getEnergy) + 0.75 * d.getEnergy
	 */
	@Override
	public Dino fight(Dino d) {
		int myForce = this.energy * this.dimension;
		int hisForce;
		if (d instanceof Carnivorous)
			hisForce = 2 * d.energy * d.dimension;
		else hisForce = d.energy * d.dimension;
		
		if (myForce >= hisForce){
			return this;
		}
		
		if (d instanceof Carnivorous)
				d.energy += (int)(0.75 * d.energy);
		
		return d;
	}
}

